package com.zbkj.crmeb.merchant.admin.controller;

import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.zbkj.crmeb.agent.request.AdminAgentRequest;
import com.zbkj.crmeb.agent.response.AgentInfoResponse;
import com.zbkj.crmeb.chant.model.ChannelMerchant;
import com.zbkj.crmeb.chant.request.MerchantListRequest;
import com.zbkj.crmeb.chant.vo.ChannelMerchantVO;
import com.zbkj.crmeb.merchant.admin.service.MerAgentUserService;
import com.zbkj.crmeb.user.request.AgentUserRequest;
import com.zbkj.crmeb.user.response.TopDetail;
import com.zbkj.crmeb.user.response.UserResponse;
import com.zbkj.crmeb.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务商-用户管理
 *
 * @author lvmd
 * @date 2022-02-18
 */
@RestController
@RequestMapping("api/admin/mer/agent/user")
@Api(tags = "服务商-用户管理")
public class MerAgentUserController {

    @Resource
    private MerAgentUserService agentUserService;
    @Resource
    private UserService userService;

    /**
     * 服务商用户列表
     *
     * @param searchRequest    搜索条件
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "服务商用户列表")
    @GetMapping("/userList")
    public CommonResult<CommonPage<UserResponse>> listAgentUsers(@ModelAttribute AgentUserRequest searchRequest,
                                                                 @ModelAttribute PageParamRequest pageParamRequest) {

        CommonPage<UserResponse> userCommonPage = CommonPage.restPage(agentUserService.listAgentUsers(searchRequest, pageParamRequest));
        return CommonResult.success(userCommonPage);
    }

    /**
     * 服务商商户列表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @param request
     * @return
     */
    @ApiOperation(value = "服务商商户列表")
    @GetMapping("/merList")
    public CommonResult<CommonPage<ChannelMerchantVO>> listAgentMerchants(@ModelAttribute MerchantListRequest request,
                                                                          @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<ChannelMerchantVO> userCommonPage = CommonPage.restPage(agentUserService.listAgentMerchants(request, pageParamRequest));
        return CommonResult.success(userCommonPage);
    }

    /**
     * 分页显示服务商
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/agentList", method = RequestMethod.GET)
    public CommonResult<CommonPage<AgentInfoResponse>> listAgents(@ModelAttribute AgentUserRequest request,
                                                                  @ModelAttribute PageParamRequest pageParamRequest) {

        CommonPage<AgentInfoResponse> userCommonPage = CommonPage.restPage(agentUserService.listAgents(request, pageParamRequest));
        return CommonResult.success(userCommonPage);
    }

    /**
     * 服务商用户详情
     *
     * @param userId uid
     */
    @ApiOperation(value = "服务商用户详情")
    @GetMapping("/userDetail")
    public CommonResult<UserResponse> getAgentUserDetail(@RequestParam(name = "userId") Integer userId) {
        UserResponse response = agentUserService.getAgentUserDetail(userId);
        return CommonResult.success(response);
    }

    /**
     * 服务商商户详情
     *
     * @param merchantId 商户id
     */
    @ApiOperation(value = "服务商商户详情")
    @GetMapping("/merDetail")
    public CommonResult<ChannelMerchantVO> getAgentMerchantDetail(@RequestParam(name = "merchantId") Integer merchantId) {

        return CommonResult.success(agentUserService.getAgentMerchantDetail(merchantId));
    }

    /**
     * 服务商商户详情
     *
     * @param agentId 服务商id
     */
    @ApiOperation(value = "服务商详情")
    @GetMapping("/agentDetail")
    public CommonResult<AgentInfoResponse> getAgentDetail(@RequestParam(name = "agentId") Integer agentId) {
        return CommonResult.success(agentUserService.getAgentDetail(agentId));
    }

    /**
     * 根据参数类型查询会员对应的信息
     *
     * @param userId           Integer 会员id
     * @param type             int 类型 0=消费记录，1=积分明细，2=签到记录，3=持有优惠券，4=余额变动，5=好友关系
     * @param pageParamRequest PageParamRequest 分页
     */
    @ApiOperation(value = "服务商用户账户详情")
    @RequestMapping(value = "/infobycondition", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", example = "1", required = true),
            @ApiImplicitParam(name = "type", value = "0=消费记录，1=积分明细，2=签到记录，3=持有优惠券，4=余额变动，5=好友关系", example = "0"
                    , required = true)
    })
    public CommonResult<CommonPage<T>> infoByCondition(@RequestParam(name = "userId") @Valid Integer userId,
                                                       @RequestParam(name = "type") @Valid @Max(5) @Min(0) int type,
                                                       @ModelAttribute PageParamRequest pageParamRequest) {
        if (type == 3) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }
        return CommonResult.success(CommonPage.restPage((List<T>) userService.getInfoByCondition(userId, type, pageParamRequest)));
    }

    /**
     * 获取会员详情对应数据
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "服务商用户账户详情页Top数据")
    @RequestMapping(value = "/topdetail", method = RequestMethod.GET)
    public CommonResult<TopDetail> topDetail(@RequestParam @Valid Integer userId) {

        return CommonResult.success(userService.getTopDetail(userId));
    }

    /**
     * 商户下拉框
     *
     * @param request 搜索条件
     */
    @ApiOperation(value = "服务商-商户下拉框")
    @GetMapping(value = "/mer/selectList")
    public CommonResult<List<ChannelMerchant>> listChannelMerchants(@Validated AdminAgentRequest request) {
        return agentUserService.listChannelMerchants(request);

    }

}



